package cn.hyy.FVOnline.controller;

import cn.hyy.FVOnline.entity.PageEntitys;
import cn.hyy.FVOnline.entity.Shop;
import cn.hyy.FVOnline.service.ShopService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

@Controller
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @RequestMapping(value = "/getShop", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getShop(@RequestParam("userId") String userId){

        Shop shop = new Shop();
        shop.setUserId(Integer.parseInt(userId));

        Shop s = shopService.findByUserId(shop);

        return JSON.toJSONString(s);
    }

    @RequestMapping(value = "/newShop", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String newShop(@RequestParam(value = "userId") String userId,@RequestParam(value = "shopname") String shopName,@RequestParam(value = "reason") String reason,@RequestParam(value = "file",required=false) MultipartFile file){

        JSONObject jo = new JSONObject();
        try {
            Shop shop = new Shop();
            shop.setUserId(Integer.parseInt(userId));
            shop.setShopname(shopName);
            shop.setReason(reason);
            shopService.newShop(shop,file);
            jo.put("code","success");
            jo.put("message","提交申请成功");
        }catch (Exception e){
            jo.put("code","error");
            jo.put("message","提交申请失败");
        }
        return jo.toJSONString();
    }

    @RequestMapping("/getHS")
    public @ResponseBody void getHS(@RequestParam("userId") String userId, HttpServletResponse response){

        try {

            BufferedImage bi = shopService.findHS(userId);

            ImageIO.write(bi,"PNG",response.getOutputStream());
        }catch (Exception e){
        }

    }

    @RequestMapping(value = "/findById", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String findById(@RequestParam("shopid") String shopid){
        Shop shop = shopService.findById(Integer.parseInt(shopid));
        return JSON.toJSONString(shop);
    }

    @RequestMapping(value = "/getShopList", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getPageShopList(@RequestParam("what") String name,@RequestParam("page") String page,@RequestParam("pageSize") String pageSize){

        PageEntitys pageShopList = shopService.findPageShopList(name, Integer.parseInt(page), Integer.parseInt(pageSize));
        return JSON.toJSONString(pageShopList);
    }

    @RequestMapping(value = "/delShop", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String delShop(@RequestParam("id") String id){
        JSONObject jo = new JSONObject();
        try {
            shopService.delShop(Integer.parseInt(id));
            jo.put("code","success");
            jo.put("message","店铺封禁成功！");
        }catch (Exception e){
            jo.put("code","error");
            jo.put("message","店铺封禁失败！");
        }
        return jo.toJSONString();
    }


    @RequestMapping(value = "/getShopList2", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getPageShopList2(@RequestParam(value = "page",required = false) String page,@RequestParam(value = "pageSize",required = false) String pageSize){
        PageEntitys pageShopList = shopService.findPageShopList2(Integer.parseInt(page), Integer.parseInt(pageSize));
        return JSON.toJSONString(pageShopList);
    }


    @RequestMapping(value = "/addShop", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String addShop(@RequestParam("id") String id){
        JSONObject jo = new JSONObject();
        try {
            shopService.addShop(Integer.parseInt(id));
            jo.put("code","success");
            jo.put("message","店铺审核成功！");
        }catch (Exception e){
            jo.put("code","error");
            jo.put("message","店铺审核失败！");
        }
        return jo.toJSONString();
    }

}
